import 'package:flutter/material.dart';
import 'package:x3_base_core/base_models/base_key_value_model.dart';
import 'package:x3_component_core/x3_component_core.dart';

/// author：william on 2021/4/1 18:06
/// email：362661044@qq.com

class BusinessCommonSelectItem<T extends BaseKeyValueModel> extends StatefulWidget {
  final bool isSelected;
  final T model;
  final Widget child;
  final Function(bool, T) onChange;
  const BusinessCommonSelectItem({Key key, this.isSelected = false, @required this.child, @required this.onChange, @required this.model}) : super(key: key);
  @override
  _BusinessCommonSelectItemState createState() => _BusinessCommonSelectItemState<T>();
}

class _BusinessCommonSelectItemState<T extends BaseKeyValueModel> extends State<BusinessCommonSelectItem> with AutomaticKeepAliveClientMixin {
  bool _selected;
  T get _model => widget.model;

  @override
  void initState() {
    _selected = widget.isSelected;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    if (_model == null) return SizedBox();
    return BaseCardComponent(
      padding: const EdgeInsets.only( top: 12.0, bottom: 12.0, left: 16.0, right: 4 ),
      onTap: () {
        _selected = !_selected;
        if (widget.onChange != null) widget.onChange(_selected, _model);
        setState(() {});
      },
      child: IntrinsicHeight(
        child: Row(
          children: [
            Expanded(child: widget.child ?? SizedBox()),
            SizedBox(width: 8),
            BaseCustomCheckboxButton(checked: _selected, disable: true),
          ],
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
